package com.bhs.sansong.ui.findLogistics;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.RangeNameAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.RangeName;
import com.bhs.sansong.bean.RangeTransportBean;
import com.bhs.sansong.net.NetCallBack;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 运输范围
 */
public class RangeTransportActivity extends BaseActivity implements NetCallBack {

    private RecyclerView mRvProvince;
    private RangeNameAdapter mProvinceAdapter;
    private List<RangeName> mProvinceList = new ArrayList<>();
    private RecyclerView mRvCity;
    private RangeNameAdapter mCityAdapter;
    private List<RangeName> mCityList = new ArrayList<>();
    private RecyclerView mRvCounty;
    private RangeNameAdapter mCountyAdapter;
    private List<RangeName> mCountyList = new ArrayList<>();
    private String end_province;
    private String end_city;
    private String end_county;
    private String[] endCountys;

    @Override
    public int initLayout() {
        return R.layout.activity_range_transport;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("查询运输范围");
        mRvProvince = findViewById(R.id.rv_province);
        mRvCity = findViewById(R.id.rv_city);
        mRvCounty = findViewById(R.id.rv_county);
        networkRequest(netApi.getSpecialLineRange(getIntent().getIntExtra("logistics_id",0)), this);
        TextView mTvLineName = findViewById(R.id.tv_line_name);
        end_city = getIntent().getStringExtra("end_city");
        end_province = getIntent().getStringExtra("end_province");
        end_county = getIntent().getStringExtra("end_county");
        if(!TextUtils.isEmpty(end_city)) {
            endCountys = end_county.split(",");
            mTvLineName.setText(String.format("线路名称：%s专线", end_city));
        }else {
            mTvLineName.setVisibility(View.GONE);
        }

    }

    @Override
    public void initData() {
        mProvinceAdapter = new RangeNameAdapter(1, mProvinceList);
        mCityAdapter = new RangeNameAdapter(2, mCityList);
        mCountyAdapter = new RangeNameAdapter(3, mCountyList);
        mRvProvince.setAdapter(mProvinceAdapter);
        mRvCity.setAdapter(mCityAdapter);
        mRvCounty.setAdapter(mCountyAdapter);
        mRvProvince.setLayoutManager(new LinearLayoutManager(this));
        mRvCity.setLayoutManager(new LinearLayoutManager(this));
        mRvCounty.setLayoutManager(new LinearLayoutManager(this));

        mProvinceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mCityList.clear();
                mCityList.addAll(mProvinceList.get(position).getChild());
                mCountyList.clear();
                mCountyList.addAll(mCityList.get(0).getChild());
                mCityAdapter.notifyDataSetChanged();
                mCountyAdapter.notifyDataSetChanged();
            }
        });

        mCityAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mCountyList.clear();
                mCountyList.addAll(mCityList.get(position).getChild());
                mCountyAdapter.notifyDataSetChanged();
            }
        });
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        RangeTransportBean bean = new Gson().fromJson(json, RangeTransportBean.class);
        if (bean.isStatus()) {
            mProvinceList.addAll(bean.getData().getRange());
            for (RangeName rangeName : mProvinceList) {
                if (rangeName.getName().equals(end_province)) {
                    rangeName.setCheck(true);
                    mCityList.addAll(rangeName.getChild());
                    break;
                }
            }
            if(TextUtils.isEmpty(end_city)){
                mCityList.addAll(mProvinceList.get(0).getChild());
            }else {
                for (RangeName rangeName : mCityList) {
                    if (rangeName.getName().equals(end_city)) {
                        rangeName.setCheck(true);
                        mCountyList.addAll(rangeName.getChild());
                        break;
                    }
                }
            }

            if(TextUtils.isEmpty(end_county)){
                mCountyList.addAll(mCityList.get(0).getChild());
            }else {
                for (RangeName rangeName : mCountyList) {
                    boolean isHas = false;
                    for (String endCounty : endCountys) {
                        if (rangeName.getName().equals(endCounty)) {
                            isHas = true;
                            break;
                        }
                    }
                    rangeName.setCheck(isHas);
                }
            }
            mProvinceAdapter.notifyDataSetChanged();
            mCityAdapter.notifyDataSetChanged();
            mCountyAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}